/*
	Author: 		夏の寒风
	Version:		1.0 (2013/09/27)
	QQ:				490720430
*/
(function($,window){
	$.fn.jsfoot_move = function(options){
		var o = $.extend({},$.fn.jsfoot_move.options, options || {}),keyMoveTarget;
		var $modules = $(o.moveModules);
		
		$modules.each(function() {
			var baseParent = $(this);
			
			var $mbox = baseParent.find(o.moveBox);
			
			var btnPlus = baseParent.find(o.btnItem.plus);
			var btnMinus = baseParent.find(o.btnItem.minus);
			var btnAltitude = baseParent.find(o.btnItem.altitude);
			var btnAdd = $(".quickmethod .btner");
			
			var timer1,timer2;
			btnPlus.mousedown(function() {
				timer1 = setTimeout(function() {
					timer2 = setInterval(function() {
						resizeHeight(baseParent,'+');
					},20);
				},500);
				resizeHeight(baseParent,'+');
			});
			btnPlus.mouseup(function() {
				clearTimeout(timer1);
				clearInterval(timer2);
			});
			btnMinus.mousedown(function() {
				timer1 = setTimeout(function() {
					timer2 = setInterval(function() {
						resizeHeight(baseParent,'-');
					},20);
				},500);
				resizeHeight(baseParent,'-');
			});
			btnMinus.unbind('mouseup').mouseup(function() {
				clearTimeout(timer1);
				clearInterval(timer2);
			});
			btnAdd.unbind('click').click(function() {
				var _url = btnAdd.attr('ajax-href');
				var _data = btnAdd.attr('ajax-data');
				var _title = btnAdd.attr('ajax-title');
				if(!_url) return false;
				$.fn.jsfoot_dialog({
					width: 780,
					height: 500,
					bgvisibel: true,
					title: _title,
					html: '',
					ajax: {
						url: _url,
						data: _data
					}
				});
			});
		});
		
		var getMinHeight = function(baseParent) {
			//var baseParent = base.parent();
			var pHeight = 0;
			$(baseParent).find(o.moveBox).each(function() {
				var _pHeight = $(this).position().top + $(this).height();
				pHeight = _pHeight > pHeight ? _pHeight : pHeight;
			});
			return pHeight;
		};
		
		var resizeHeight = function(baseParent,type) {
			//var baseParent = base.parent();
			var btnAltitude = baseParent.find(o.btnItem.altitude);
			var _pHeight = baseParent.height();
			if('+' == type) {
				_pHeight++;
			}
			if('-' == type) {
				_pHeight--;
				var minHeight = getMinHeight(baseParent);
				_pHeight = _pHeight < minHeight ? minHeight : _pHeight;
			}
			if('==' == type) {
				_pHeight = getMinHeight(baseParent);
			}
			baseParent.height(_pHeight);
			btnAltitude.html(_pHeight);
		};

		/******************************************************************拖动处理类******************************************************************/
		var moveFunction = function() {
			var lastTop = 0,
				isDown = false;
				moveBox = $(o.moveBox),
				barTop = moveBox.find(o.barItem.top),
				barDown = moveBox.find(o.barItem.down);
				
			/* 模块上移 */
			barTop.live('click', function() {
				handelMoveingDone($(this).parents(o.moveBox), -1);
			});
			
			/* 模块下移 */
			barDown.live('click', function() {
				handelMoveingDone($(this).parents(o.moveBox), 1);
			});

			/* 鼠标进入处理 */
			moveBox.live("mouseenter", function(c){
				var $this = $(this),
					keyMove = 0;
				
				$(document).bind('keydown', 'up', function(evt) {
					if(isDown) return;
					lastTop = parseInt($this.css('margin-top'));
					if(lastTop + keyMove > 0) keyMove--;
					handelMove($this, keyMove);
					return false;
				});
				$(document).bind('keydown', 'down', function(evt) {
					if(isDown) return;
					lastTop = parseInt($this.css('margin-top'));
					keyMove++;
					handelMove($this, keyMove);
					return false;
				});
			});
			/* 鼠标移出处理 */
			moveBox.live("mouseleave", function(c){
				$(document).unbind('keydown');
			});
			/* 鼠标按下处理 */
			moveBox.live('mousedown', function(e) {
				e.preventDefault();
				isDown = true;
				if($(this).hasClass('dismoved')) return;
				var eX = e.pageX,
					eY = e.pageY,
					$this = $(this);
				lastTop = parseInt($this.css('margin-top'));
				$(document).bind("mousemove",function(e) {
					var _eY = e.pageY - eY;
					handelMove($this, _eY);
				});
				$(document).bind("mouseup",function(){
					isDown = false;
					$(document).unbind("mousemove");
					$(document).unbind("mouseup");
				});
			});
					
			var handelMove = function(dom, moveY) {
				var top = lastTop + moveY;
				top = top < 0 ? 0 : top;
				dom.css({marginTop: top});
			}
					
			var handelMoveingDone = function(dom, result) {
				if(result == -1) {
					if(dom.index() == 0) return;
					dom.after(dom.prev());
				}
				if(result == 1) {
					if(dom.index() == $(o.moveBox).length - 1) return;
					dom.before(dom.next());
				}
			}
		}
		moveFunction();

		/******************************************************************模块编辑弹层******************************************************************/
		$("body").delegate(o.moveBox+' '+o.barItem.edit, 'click', function() {
			//记录当前操作的module_index
			$("#current_index").val($(this).parents('.barbox').index());
			//保存当前样式
			var current_html = $(this).parents('.barbox').find('[id^=module_body_]');
			$("#current_html").html(current_html.clone());
			//弹出编辑层
			var _url = $(this).attr('ajax-href');
			var _data = $(this).attr('ajax-data');
			var _title = $(this).attr('ajax-title');
			if(!_url) return false;
			$.fn.jsfoot_dialog({
				width: 780,
				height: 500,
				bgvisibel: true,
				title: _title,
				html: '',
				ajax: {
					url: _url,
					data: _data
				}
			});
		});

		/******************************************************************模板添加******************************************************************/
		;!function(){
			var module_index = 100,
				module_category = 1;
			$(".stdmod-body .modulebox .bluebtn").live("click", function(ev){
				ev.preventDefault();

				/*加载指定模板*/
				$(".temporary_module").load($(this).attr("href"), function(response,status){
					//加载成功
					if(status=="success"){
						//先清空临时模块。避免误操作
						$(this).html("");
						//添加模块
						$(".modules").append('<div class="barbox"><input type="hidden" class="module_index" value="'+module_index+'" />'+response+'</div>');
						//更正内容
						var current = $('.modules .barbox:last-child');
						current.find("#module_html").attr({"id":"module_body_"+module_index, "data-module-index":module_index});
						current.find("#module_css").attr("id","module_script_"+module_index);
					}
				});

				/*临时处理，之后需要删除*/
				module_index++;
			});
		}();
	};
	$.fn.jsfoot_move.options = {
		moveModules: '.modules', 
		moveBox: '.barbox',
		moveMain: '.barmain',
		btnItem: {
			plus: '.btner.plus',
			minus: '.btner.minus',
			altitude: '.btner.altitude',
			add: '.btner.add'
		},
		barItem: {
			edit: '.ds-bar.ds-bar-edit',
			top: '.ds-bar.ds-bar-top',
			down: '.ds-bar.ds-bar-down',
			del: '.ds-bar.ds-bar-del'
		}
	};
})(jQuery,window);
(function($){
	$(window).load(function(){
		$(document).jsfoot_move();
	});
})(jQuery);